<template>
  <view class="myPage">
    <scroll-view
      v-if="dataList.length > 0"
      scroll-x="false"
      scroll-y="true"
      @scrolltolower="getList"
    >
      <view class="myList">
        <view
          v-for="(item, index) in dataList"
          :key="index"
          class="list-item"
          @click="selectAddress(item)"
        >
          <view class="left">
            <text class="text1">{{
              item.provinceName +"  "+ item.cityName +"  "+ item.countyName
            }}</text>
            <text class="text2">{{
              item.address
            }}</text>
            <view class="top">
              <text>{{ item.name }}&nbsp;&nbsp;{{ item.phone }}</text>
              <text v-if="item.isDefault == 1" class="label">默认</text>
            </view>
          </view>
          <image
            class="edit"
            :src="`${img_baseUrl}/statics/std/static/edit.png`"
            @click.stop="goToEdit(item)"
          ></image>
        </view>
        <view class="loadMore">{{
          loadMore ? "数据加载中" : "没有更多内容了"
        }}</view>
      </view>
    </scroll-view>
    <view class="noData" v-else style="height: 100%">
      <image :src="`${img_baseUrl}/statics/std/static/noData.png`"></image>
      <text>暂无数据</text>
    </view>
    <view
      class="footer-btn"
      @click="goToAdd"
      :style="{ bottom: (GET_SAFEAREA_BOTTOM > 0 ? GET_SAFEAREA_BOTTOM : 34) + 'px' }"
    >
      <image :src="`${img_baseUrl}/statics/std/static/add.png`"></image>
      <text>新增地址</text>
    </view>
  </view>
</template>

<script>
import { requestGet } from "@/utils/request.js";
import { mapGetters } from "vuex";
import store, { GET_SAFEAREA_BOTTOM } from "../../store/index.js";
export default {
  data() {
    return {
		img_baseUrl: process.env.VUE_APP_BASEURL_IMG_API,
      total: 0,
      dataList: [],
      pages: {
        pageNum: 1,
        pageSize: 10,
      },
      loadMore: true,
    };
  },
  props: ["form"],
  computed: {
    ...mapGetters([GET_SAFEAREA_BOTTOM]),
  },
  mounted() {
    console.log("安全区距离", this.GET_SAFEAREA_BOTTOM, store, mapGetters);
  },
  onShow() {
    this.loadMore = true;
    this.pages = {
      pageNum: 1,
      pageSize: 10,
    };
    this.dataList = [];
    this.getList();
  },
  methods: {
    selectAddress(item) {
      if (this.form) {
        this.$store.commit("SELECT_ADDRESS_INFO", item);
        uni.navigateBack();
      }
    },
    getList() {
      if (!this.loadMore) {
        return;
      }
      requestGet(
        "/mobile/receiveAddress/listBySelf",
        { ...this.pages },
        "/shop"
      )
        .then((res) => {
          console.log("获取了list列表数据", res);
          if (res.code === 200) {
            if (res.rows.length > 0) {
              this.dataList.push(...res.rows);
              if (this.dataList.length < res.total) {
                this.loadMore = true;
                this.pages.pageNum = this.pages.pageNum + 1;
              } else {
                this.loadMore = false;
              }
            }
          }
          console.log("111111111===", this.loadMore);
        })
        .catch((err) => console.log("错误了", err));
    },
    goToAdd() {
      uni.navigateTo({
        url: "/page_my/address/addOrEdit",
      });
    },
    goToEdit(item) {
      uni.navigateTo({
        url: "/page_my/address/addOrEdit?id=" + item.id,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.myPage {
  position: relative;
  height: 100vh;
  width: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  background-color: #F5F7F6;
}
.myList {
  height: calc(100vh - 152rpx);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: flex-start;
  padding: 0 32rpx;
  margin-top: 16rpx;
  .list-item {
    width: 100%;
    padding: 20rpx 0;
    height: 196rpx;
    background: #FFFFFF;
    border-radius: 24rpx 24rpx 24rpx 24rpx;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    .left {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      justify-content: center;
      margin-left: 24rpx;
      .top {
        font-size: 28rpx;
        font-weight: 400;
        color: rgba(0, 0, 0, 0.8);
        line-height: 44rpx;
        .label {
          width: 64rpx;
          height: 32rpx;
          padding: 0 8rpx;
          background: rgba(63,170,115,0.1);
          border-radius: 8rpx 8rpx 8rpx 8rpx;
          font-size: 24rpx;
          font-weight: 400;
          color: #3FAA73;
          line-height: 32rpx;
          text-align: center;
          margin-left: 16rpx;
        }
      }
      .text1 {
        font-size: 28rpx;
        font-weight: 300;
        color: rgba(0, 0, 0, 0.7);
        line-height: 36rpx;
        word-break: break-all;
        margin-bottom: 16rpx;
      }
      .text2 {
        font-size: 32rpx;
        font-weight: 600;
        color: rgba(0,0,0,0.9);
        line-height: 44rpx;
        word-break: break-all;
        margin-bottom: 16rpx;
      }
    }
    .edit {
      height: 40rpx;
      width: 40rpx;
      flex: 0 0 auto;
      margin-left: 42rpx;
      margin-right: 24rpx;
    }
  }
  .loadMore {
    display: block;
    width: 100%;
    font-size: 24rpx;
    color: rgba(0, 0, 0, 0.5);
    text-align: center;
    line-height: 64rpx;
  }
}
.footer-btn {
  position: fixed;
  bottom: 34rpx;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  width: 598rpx;
  height: 84rpx;
  background: #3FAA73;
  border-radius: 44rpx 44rpx 44rpx 44rpx;
  image {
    width: 40rpx;
    height: 40rpx;
    flex: 0 0 auto;
  }
  text {
    font-size: 32rpx;
    font-weight: 400;
    color: #ffffff;
    line-height: 32rpx;
  }
}
</style>
